package guilayer;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import modellayer.Product;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import controllayer.ProductCtrl;

public abstract class AbstractSetProduct extends JPanel {
	protected ProductCtrl productCtrl;
	protected Product product;
	private JTextField txtProductBarcode;
	private JLabel lblProductBarcodeError;
	private JPanel pnlMain;
	private String productBarcode;
	private JLabel lblProductNoError;
	private String productNo;
	private JTextField txtProductNo;

	/**
	 * Create the panel.
	 */
	public AbstractSetProduct() {
		productCtrl = ProductCtrl.getInstance();
		
		pnlMain = getPanelForm();
		// Remove the background
		pnlMain.setOpaque(false);
		add(pnlMain);
	}

	public AbstractSetProduct(String barcode) {
		productCtrl = ProductCtrl.getInstance();
		product = productCtrl.getProductFromBarcode(barcode);
		
		if (product==null) {
			pnlMain = getPanelForm();
			showErrorLabelProductBarcode(true);
		} else {
			//pnlMain.removeAll();
			setProductBarcode(barcode);
			//customerNo = customer.getCustomerNo();
			pnlMain = loadThePage();
			//pnlMain.updateUI();
		}
		
		add(pnlMain);
	}

	private JPanel getPanelForm() {
		JPanel pnlForm = new JPanel();
		// Remove the background
		pnlForm.setOpaque(false);
		
		pnlForm.setBorder(new TitledBorder(null, "Indtast produktets stregkode"));
		pnlForm.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		lblProductBarcodeError = new JLabel("Den indtastede stregkode kunne ikke findes!");
		lblProductBarcodeError.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
		lblProductBarcodeError.setForeground(Color.RED);
		pnlForm.add(lblProductBarcodeError, "4, 2");
		
		showErrorLabelProductBarcode(false);
		
		JLabel lblProductBarcode = new JLabel("Stregkode:");
		pnlForm.add(lblProductBarcode, "2, 4, right, default");
		
		txtProductBarcode = new JTextField();
		txtProductBarcode.requestFocus();
		txtProductBarcode.requestFocusInWindow();
		txtProductBarcode.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyChar()==KeyEvent.VK_ENTER) {
					setProductAndChangePanel(txtProductBarcode.getText(), true);
				}
			}
		});
		pnlForm.add(txtProductBarcode, "4, 4, fill, default");
		txtProductBarcode.setColumns(10);
		
		JButton btnGoLookUpProduct = new JButton("Videre");
		btnGoLookUpProduct.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (txtProductBarcode.getText().length()==8 || txtProductNo.getText().length() == 0) {
					setProductAndChangePanel(txtProductBarcode.getText(), true);
				} else {
					setProductAndChangePanel(txtProductNo.getText(), false);
				}
			}
		});
		
		JLabel label = new JLabel("");
		pnlForm.add(label, "2, 6");
		
		JLabel lblSeperator = new JLabel("--- eller ---");
		pnlForm.add(lblSeperator, "4, 6, center, default");
		
		lblProductNoError = new JLabel("Det indtastede produktnr. kunne ikke findes!");
		lblProductNoError.setForeground(Color.RED);
		pnlForm.add(lblProductNoError, "4, 8");
		showErrorLabelProductNo(false);
		
		JLabel lblProductNo = new JLabel("Produktnr.");
		pnlForm.add(lblProductNo, "2, 10, right, default");
		
		txtProductNo = new JTextField();
		txtProductNo.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyChar()==KeyEvent.VK_ENTER) {
					setProductAndChangePanel(txtProductNo.getText(), false);
				}
			}
		});
		pnlForm.add(txtProductNo, "4, 10, fill, default");
		txtProductNo.setColumns(10);
		pnlForm.add(btnGoLookUpProduct, "1, 12, 4, 1");
		return pnlForm;
	}
	
	private void showErrorLabelProductBarcode(boolean show) {
		lblProductBarcodeError.setVisible(show);
	}
	
	private void showErrorLabelProductNo(boolean show) {
		lblProductNoError.setVisible(show);
	}

	private void setProductAndChangePanel(String data, boolean isBarcode) {
		showErrorLabelProductBarcode(false);
		showErrorLabelProductNo(false);
		
		if (isBarcode) {
			product = productCtrl.getProductFromBarcode(data);
		} else {
			product = productCtrl.getProductFromProductNo(data);
		}
		
		if (product==null) {
			if (isBarcode) {
				showErrorLabelProductBarcode(true);
			} else {
				showErrorLabelProductNo(true);
			}
		} else {
			setProductBarcode(data);
			pnlMain.removeAll();
			pnlMain.setVisible(false);
			//pnlMain = null;
			pnlMain = loadThePage();
			pnlMain.setVisible(true);
			pnlMain.updateUI();
		}
	}
	
	public String getProductNo() {
		return productNo;
	}

	public void setProductNo(String productNo) {
		this.productNo = productNo;
	}

	public String getProductBarcode() {
		return productBarcode;
	}

	public void setProductBarcode(String barcode) {
		this.productBarcode = barcode;
	}

	protected void callRootPane(int leftMenu, int mainContent, Object data) {
		((MainWindow)(this.getRootPane().getParent())).refreshAll(leftMenu, mainContent, data);
	}
	
	protected abstract JPanel loadThePage();
}
